/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.LabAuthorization;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.LabAuthorizationRepository;
import me.zhengjie.modules.biz.service.LabAuthorizationService;
import me.zhengjie.modules.biz.service.dto.LabAuthorizationDto;
import me.zhengjie.modules.biz.service.dto.LabAuthorizationQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.LabAuthorizationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class LabAuthorizationServiceImpl implements LabAuthorizationService {

    private final LabAuthorizationRepository labAuthorizationRepository;
    private final LabAuthorizationMapper labAuthorizationMapper;

    @Override
    public Map<String,Object> queryAll(LabAuthorizationQueryCriteria criteria, Pageable pageable){
        Page<LabAuthorization> page = labAuthorizationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(labAuthorizationMapper::toDto));
    }

    @Override
    public List<LabAuthorizationDto> queryAll(LabAuthorizationQueryCriteria criteria){
        return labAuthorizationMapper.toDto(labAuthorizationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LabAuthorizationDto findById(Integer id) {
        LabAuthorization labAuthorization = labAuthorizationRepository.findById(id).orElseGet(LabAuthorization::new);
        ValidationUtil.isNull(labAuthorization.getId(),"LabAuthorization","id",id);
        return labAuthorizationMapper.toDto(labAuthorization);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LabAuthorizationDto create(LabAuthorization resources) {
        return labAuthorizationMapper.toDto(labAuthorizationRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LabAuthorization resources) {
        LabAuthorization labAuthorization = labAuthorizationRepository.findById(resources.getId()).orElseGet(LabAuthorization::new);
        ValidationUtil.isNull( labAuthorization.getId(),"LabAuthorization","id",resources.getId());
        labAuthorization.copy(resources);
        labAuthorizationRepository.save(labAuthorization);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            labAuthorizationRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LabAuthorizationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LabAuthorizationDto labAuthorization : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("人员", labAuthorization.getUserId());
            map.put("实验室", labAuthorization.getLabId());
            map.put("授权人员", labAuthorization.getAuthUserId());
            map.put("期限", labAuthorization.getExpiredAt());
            map.put(" createdAt",  labAuthorization.getCreatedAt());
            map.put(" updatedAt",  labAuthorization.getUpdatedAt());
            map.put("是否删除", labAuthorization.getIsDeleted());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}